package org.agile4j.plugin.gun.xml;

import java.io.File;
import java.util.Map;

import org.agile4j.plugin.gun.helper.GunHelper;
import org.agile4j.plugin.gun.utils.ParserUtil;
import org.agile4j.plugin.gun.utils.ReplaceContainerUtil;
import org.agile4j.plugin.gun.xml.baseenumtype.RestrictionType;
import org.agile4j.plugin.gun.xml.baseenumtype.Schema;
import org.agile4j.plugin.gun.xml.baseenumtype.Subenum;
import org.agle4j.framework.annotation.Service;
import org.agle4j.framework.utils.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class GunEnumBaseType {
	private static final Logger LOGGER = LoggerFactory.getLogger(GunEnumBaseType.class);
	private static final String PREFIX = "E_" ;
	
	public boolean createTarFile() {
		return createTarFile(GunHelper.getCreateTarXmlFileUrl()) ;
	}
	
	public boolean createTarFile(String createTarFileUrl) {
		Map<String, Schema> schemaMap = ParserUtil.deplogyBaseEnumBaseType(new File(createTarFileUrl));
		if (CollectionUtil.isNotEmpty(schemaMap)) {
			for (Map.Entry<String, Schema> schemaEntry : schemaMap.entrySet()) {
				Schema schema = schemaEntry.getValue();
				if (CollectionUtil.isNotEmpty(schema.getRestrictionType())) {
					for (RestrictionType restrictionType : schema.getRestrictionType()) {
						String oldId = restrictionType.getId() ;
						String key = StringUtils.remove(oldId, PREFIX).toLowerCase() ;
						if (ReplaceContainerUtil.containsKey(key)) {
							String newId = PREFIX + ReplaceContainerUtil.getReplaceValue(key).toUpperCase() ;
							restrictionType.setId(newId);
							/////////////Map中添加  type
							ReplaceContainerUtil.putRepalceValue(oldId, newId);
						}
						
						if (ReplaceContainerUtil.containsKey(restrictionType.getBase())) {	//	赋值BASE
							restrictionType.setBase(ReplaceContainerUtil.getReplaceValue(restrictionType.getBase()));
						}
						
						// 需要定义另一套替换规则的Map
//						if (CollectionUtil.isNotEmpty(restrictionType.getEnumeration())) {	// enumeration.id 的更新
//							for (Enumeration enumeration : restrictionType.getEnumeration()) {
//								if (ReplaceContainerUtil.containsKey(enumeration.getId())) {
//									enumeration.setId(ReplaceContainerUtil.getReplaceValue(enumeration.getId()));
//								}
//							}
//						}
						
						if (restrictionType.getSubenums() != null) {
							if (CollectionUtil.isNotEmpty(restrictionType.getSubenums().getSubenum())) {
								for (Subenum subenum : restrictionType.getSubenums().getSubenum()) {
									String oldId1 = subenum.getId() ;
									String key1 = StringUtils.remove(oldId1, PREFIX).toLowerCase() ;
									if (ReplaceContainerUtil.containsKey(key1)) {
										String newId = PREFIX + ReplaceContainerUtil.getReplaceValue(key1).toUpperCase() ;
										subenum.setId(newId);
										
										/////////////Map中添加  type
										ReplaceContainerUtil.putRepalceValue(oldId1, newId);
									}
								}
							}
						}
					}
				}
				File file = new File(schemaEntry.getKey());
				String tarFilePath = file.getParent() + File.separator + GunHelper.getTarFileFormat() + file.getName(); // 目标文件路径
				ParserUtil.replaceXmlField(tarFilePath, schema);
			}
		}
		return true ;
	}
	
	public static void main(String[] args) {
		String ref = "CmDict.Comm.dianzggh" ;
		ref = StringUtils.substring(ref, StringUtils.lastIndexOf(ref, ".") + 1, StringUtils.length(ref)) ;
		LOGGER.debug(ref);
	}
}
